package com.jintoufs.service.baseinfo.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jhj.common.redis.JhjRedis;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.baseinfo.TickerDAO;
import com.jintoufs.domain.baseinfo.Ticker;
import com.jintoufs.domain.constants.RedisConstants;
import com.jintoufs.domain.ticker.TickerVO;
import com.jintoufs.service.baseinfo.TickerService;
import com.jintoufs.util.http.HttpClientUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author xiaoemo
 */
@Repository
public class TickerServiceImpl implements TickerService {
    @Resource
    private TickerDAO tickerDAO;
    @Resource
    private JhjRedis jhjRedis;

    @Override
    public List<Map<String, Object>> queryByParams(Map<String, Object> params) {
        return tickerDAO.queryByParams(params);
    }

    @Override
    public List<Map<String, Object>> queryBySite(String coinCode, String siteName) {
        Map<String, Object> map = new HashMap();
        map.put("coinCode", coinCode);
        map.put("siteName", siteName);
        return tickerDAO.queryBySite(map);
    }

    @Override
    public List<TickerVO> findByTicker(Map<String, Object> params) {
        return tickerDAO.findByTicker(params);
    }

    @Override
    public void deleteByParams(Map<String, Object> params) {
        tickerDAO.deleteByParams(params);
    }

    @Override
    public void insert(Ticker entity) {
        if (entity.getLast() != null && entity.getLast().doubleValue() > 0) {
            tickerDAO.insert(entity);
        }
    }

    @Override
    public void updateByPrimaryKey(Ticker entity) {
        tickerDAO.updateByPrimaryKey(entity);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        tickerDAO.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> getByFilter(QueryFilters filters) {
        return tickerDAO.getByFilter(filters);
    }

    @Override
    public Ticker selectByPrimaryKey(Long id) {
        return tickerDAO.selectByPrimaryKey(id);
    }


    @Override
    public void getBinanceSymbols() {
        String URL = "https://api.binance.com//api/v3/ticker/price";
        String json = HttpClientUtils.http_get("", URL);
        List<String> symbols = new ArrayList<>();
        List<JSONObject> array = JSON.parseArray(json, JSONObject.class);
        for (JSONObject jsonObject : array) {
            symbols.add(jsonObject.get("symbol").toString());
        }
        jhjRedis.put("BINANCE_SYMBOLS", symbols, RedisConstants.WEEK_SECONDS);
    }


    @Override
    public List<Map<String, Object>> getLastestPrice(Map<String, Object> params) {
        return tickerDAO.getLastestPrice(params);
    }
}
